<?php
class mastersupplier_model extends CI_Model{
	private $Kodesupplier;
    private $suppliername;
    private $Address;
    private $NoTelp;
  	private $Email;
	private $NoHP;
  	private $Websitename;
	private $ContactPerson;
    private $Createdby;
    private $Createdon;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	function setKodesupplier($kdsupp){
		$this->Kodesupplier = $kdsupp;
	}
	function getKodesupplier(){
		return $this->Kodesupplier;
	}
	
	function setsuppliername($suppname){
		$this->suppliername = $suppname;
	}
	function getsuppliername(){
		return $this->suppliername;
	}
	
	function setAddress($addr){
		$this->Address = $addr;
	}
	function getAddress(){
		return $this->Address;
	}
	
	function setContactPerson($cntperson){
		$this->ContactPerson = $cntperson;
	}
	function getContactPerson(){
		return $this->ContactPerson;
	}
	
	function setNoTlp($notelp){
		$this->NoTelp = $notelp;
	}
	function getNoTlp(){
		return $this->NoTelp;
	}
	
	function setEmail($email){
		$this->Email = $email;
	}
	function getEmail(){
		return $this->Email;
	}
	
	function setNoHp($NoHp){
		$this->NoHp = $NoHp;
	}
	function getNoHp(){
		return $this->NoHp;
	}
	
	function setWebsitename($Websitename){
		$this->Websitename = $Websitename;
	}
	function getWebsitename(){
		return $this->Websitename;
	}
	
	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}
	function getCreatedBy(){
		return $this->createdby;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}
	function getCreatedOn(){
		return $this->createdon;
	}

	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->lastupdateby;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->active = $active_val;
	}
	function getActive(){
		return $this->active;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getsupplierCode()
	{
		$query = $this->db->query("select MAX(RIGHT(Kodesupplier,4)) as kdsupp from supplier");
		$kd = "";
		if($query->num_rows()>0)
		{
			foreach($query->result() as $idsupp)
			{
				$tmpsupp = ((int)$idsupp->kdsupp)+1;
				$kd = sprintf("%04s", $tmpsupp);
			}
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "KS".$kd; 
		
		return $kd;
	}
	
	function insert(){
		$query = $this->db->query("select MAX(RIGHT(Kodesupplier,4)) as kdsupp from supplier");
		$kd = "";
		if($query->num_rows()>0)
		{
			foreach($query->result() as $idsupp)
			{
				$tmpsupp = ((int)$idsupp->kdsupp)+1;
				$kd = sprintf("%04s", $tmpsupp);
			}
		}
		else
		{
			$kd = "0001";
		}
			
		$kdsuppok="KS".$kd;
		$this->setKodesupplier($kdsuppok);
		
		$data = array(
			'Kodesupplier' => $this->getKodesupplier(),
			'suppliername' => $this->getsuppliername(),
			'Address' => $this->getAddress(),
			'NoTlp' => $this->getNoTlp(),
			'Email' => $this->getEmail(),
			'NoHp' => $this->getNoHp(),
			'Websitename' => $this->getWebsitename(),
			'ContactPerson' => $this->getContactPerson(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => $this->getLastupdateby(),
			'Lastupdateon' => $this->getLastupdateon(),
			'Active' => '1'					
		);
		
		$this->db->insert('supplier',$data);
	}
	
	function ubahmastersupplier() 
	{
            $data = array(
                'suppliername' => $this->getsuppliername(),
                'Address' => $this->getAddress(),
                'NoTlp' => $this->getNoTlp(),
                'Email' => $this->getEmail(),
				'NoHp' => $this->getNoHp(),
				'Websitename' => $this->getWebsitename(),
				'ContactPerson' => $this->getContactPerson(),
                'Lastupdateby' => $this->getLastupdateby(),
				'Lastupdateon' => $this->getLastupdateon(),
            );
 
        $this->db->where(array('Kodesupplier' => $this->getKodesupplier()));
        $this->db->update('supplier', $data);
    }

    function deletemastersupp() {
        $data = array(
            'active' => '0'
        );
        $this->db->where(array('Kodesupplier' => $this->getKodesupplier()));
        $this->db->update('supplier', $data);
    }
	
	function getList(){
		$this->db->select()->where(array('active'=>'1'));
		$this->db->order_by('Kodesupplier', 'asc');
		$query = $this->db->get('supplier');
		return $query->result();
	}
	
	function countGetList(){
		$this->db->select()->where(array('active'=>'1'));
		$this->db->order_by('Kodesupplier', 'asc');
		$query = $this->db->get('supplier');
		return $query->num_rows();
	}
	
	function getListlimit($limit,$start){
		$this->db->select()
		->where(array('active'=>'1'))
		->limit($limit,$start);
		$this->db->order_by('Kodesupplier', 'asc');
		$query = $this->db->get('supplier');
		return $query->result();
	}
	
	function getsupplierByKode($suppCode)
	{
		$this->db->select()->where(array('Kodesupplier' =>$suppCode));
		$query = $this->db->get('supplier');
		return $query->result();
	}
	
	function getkdsupp(){
		$this->db->select()->where(array('Kodesupplier' => $this->getKodesupplier(),'active'=>'1'));
		$query = $this->db->get('supplier');
		return $query->result();
	}
	
	function getkdsupplike(){
		$this->db->select()
		->where(array('Active'=>'1'))->like(array('Kodesupplier' => $this->getKodesupplier()));
		$query = $this->db->get('supplier');
		return $query->result();
	}
	
	function getnamasupplike(){
		$this->db->select()
		->where(array('Active'=>'1'))->like(array('suppliername' => $this->getsuppliername()));
		$query = $this->db->get('supplier');
		return $query->result();
	}
	
	function getwebsitelike(){
		$this->db->select()
		->where(array('Active'=>'1'))->like(array('Websitename' => $this->getWebsitename()));
		$query = $this->db->get('supplier');
		return $query->result();
	}
}